<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Lowker extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model("jobvacations_model", "jvm");
    }

    function index() {
        $perpage = 10;
        $curpage = $this->uri->segment(3);
        $where["jv_expire_date >="] = date('Y-m-d');
        $data["txtSearch"] = $this->input->post("txtSearch");
        $like = $data["txtSearch"] != "" ? array("company_name" => $data["txtSearch"]) : "";
        $data["dt"] = $this->jvm->getAll($curpage, $perpage, @$where, @$like, "jv_expire_date asc");
        $this->paging(base_url() . "lowker/index", $this->jvm->getAllCount(@$where, @$like), $perpage);
        $this->myview(@$data, "lowker/index");
    }

    function detail() {
        $data["dt"] = $this->jvm->getById($this->uri->segment(3));
        $this->mypopup("Detail Lowongan Pekerjaan", @$data, "lowker/detail");
    }

    function add() {
        if ($this->input->post()) {
            $data["low"]->title = $this->input->post("txtTitle");
            $data["low"]->officeid = $this->input->post("txtOfficeid");
            $data["low"]->location = $this->input->post("txtLocation");
            $data["low"]->qualification = $this->input->post("txtQualification");
            $data["low"]->experience = $this->input->post("txtExperience");
            $data["low"]->gender = $this->input->post("ddJenkel");
            $data["low"]->agemin = $this->input->post("ddAgemin");
            $data["low"]->agemax = $this->input->post("ddAgemax");
            $data["low"]->salary = $this->input->post("txtSalary");
            $data["low"]->education = $this->input->post("txtEducation");
            $data["low"]->note = $this->input->post("txtNote");
            $data["low"]->openuntil = $this->input->post("txtOpenuntil");

            $data["low"]->title == "" ? $data["err"]->title = strErr("mustfilled") : "";
            $data["low"]->officeid == "" ? $data["err"]->officeid = strErr("mustchoose") : "";
            $data["low"]->location == "" ? $data["err"]->location = strErr("mustfilled") : "";
            $data["low"]->qualification == "" ? $data["err"]->qualification = strErr("mustfilled") : "";
            $data["low"]->experience == "" ? $data["err"]->experience = strErr("mustfilled") : "";
            $data["low"]->agemin == "" ? $data["err"]->agemin = strErr("mustchoose") : "";
            $data["low"]->agemax == "" ? $data["err"]->agemax = strErr("mustchoose") : "";
            $data["low"]->salary == "" ? $data["err"]->salary = strErr("mustfilled") : "";
            $data["low"]->openuntil == "" ? $data["err"]->openuntil = strErr("mustfilled") : "";

            if (@$data["err"]) {
                $this->myview(@$data, "lowker/add");
            } else {
                if ($this->jvm->insert($data["low"]->title, $data["low"]->officeid, $data["low"]->location, $data["low"]->qualification, $data["low"]->experience, $data["low"]->gender, $data["low"]->agemin, $data["low"]->agemax, $data["low"]->education, $data["low"]->salary, $data["low"]->note, $data["low"]->openuntil, date('Y-m-d H:i:s'), @$this->sess->userid, date('Y-m-d H:i:s'), @$this->sess->userid)) {
                    $this->session->set_flashdata("message", "Lowongan pekerjaan berhasil disimpan");
                    $this->session->set_flashdata("redir", base_url() . "lowker");
                    redirect(base_url() . "alert/success");
                } else {
                    $this->session->set_flashdata("message", "Lowongan pekerjaan gagal disimpan");
                    $this->session->set_flashdata("redir", "http://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
                    redirect(base_url() . "alert/error");
                }
            }
        } else {
            $this->myview(@$data, "lowker/add");
        }
    }

    function edit() {
        $dt = $this->jvm->getById($this->uri->segment(3));
        if ($this->sess->isadmin == 1 || $dt->createby == $this->sess->userid) {
            if ($this->input->post()) {
                $data["low"]->title = $this->input->post("txtTitle");
                $data["low"]->officeid = $this->input->post("txtOfficeid");
                $data["low"]->location = $this->input->post("txtLocation");
                $data["low"]->qualification = $this->input->post("txtQualification");
                $data["low"]->experience = $this->input->post("txtExperience");
                $data["low"]->gender = $this->input->post("ddJenkel");
                $data["low"]->agemin = $this->input->post("ddAgemin");
                $data["low"]->agemax = $this->input->post("ddAgemax");
                $data["low"]->salary = $this->input->post("txtSalary");
                $data["low"]->education = $this->input->post("txtEducation");
                $data["low"]->note = $this->input->post("txtNote");
                $data["low"]->openuntil = $this->input->post("txtOpenuntil");

                $data["low"]->title == "" ? $data["err"]->title = strErr("mustfilled") : "";
                $data["low"]->officeid == "" ? $data["err"]->officeid = strErr("mustchoose") : "";
                $data["low"]->location == "" ? $data["err"]->location = strErr("mustfilled") : "";
                $data["low"]->qualification == "" ? $data["err"]->qualification = strErr("mustfilled") : "";
                $data["low"]->experience == "" ? $data["err"]->experience = strErr("mustfilled") : "";
                $data["low"]->agemin == "" ? $data["err"]->agemin = strErr("mustchoose") : "";
                $data["low"]->agemax == "" ? $data["err"]->agemax = strErr("mustchoose") : "";
                $data["low"]->salary == "" ? $data["err"]->salary = strErr("mustfilled") : "";
                $data["low"]->openuntil == "" ? $data["err"]->openuntil = strErr("mustfilled") : "";

                if (@$data["err"]) {
                    $this->myview(@$data, "lowker/edit");
                } else {
                    $data["low"]->updatedby = $this->sess->userid;
                    $data["low"]->updateddate = date("Y-m-d H:i:s");
                    $this->jvm->update($this->uri->segment(3), $data["low"]);
                    $this->session->set_flashdata("message", "Lowongan berhasil diubah");
                    $this->session->set_flashdata("redir", base_url() . "lowker");
                    redirect(base_url() . "alert/success");
                }
            } else {
                $data["low"] = $dt;
                $this->myview(@$data, "lowker/edit");
            }
        } else {
            $this->session->set_flashdata("message", "Anda tidak mempunyai hak akses untuk merubah data ini.");
            $this->session->set_flashdata("redir", base_url());
            redirect(base_url() . "alert/warning");
        }
    }

}

?>
